program main_corr
	
	use QMC
	
	implicit NONE
	
	integer(4), parameter		:: nmax = 1000000
	integer(4), parameter		:: nstart = 500000
	integer(4), parameter		:: navr = 128
		
	integer(4)					:: iunit, sts
	real(8)						:: tt
	real(8)						:: beta
	
	integer(4)					:: H(L)
	
	integer(4)					:: nmc
	integer(4)					:: nvr
	
	real(8)						:: y, z
	
	real(8)						:: nc(navr)
	real(8)						:: nc2(navr)
	
	real(8)						:: s, s2
	
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
	
	tt = 10.0d0
	beta = 1.0d0 / tt
	
	call Init()
	
	open( newunit = iunit, file='rescorr.txt', status = 'replace', action = 'write')
	
	H = 0
	sites = 1
	s = 0.0d0
	s2 = 0.0d0
	
	nc = 0.0d0
	nc2 = 0.0d0
	nvr = 0
	
	do nmc = 1, nmax
		call RANDOM_NUMBER(y)
		
		if ( y > 0.5) then 
			call GlobalStep (H)
		else
			call RANDOM_NUMBER(z)
			if ( z > 0.5) then
				call LocalDuoStep(beta,H)
			else
				call LocalUniStep(beta,H)
			end if
		end if
		
		if ( nmc <= nstart) cycle
		
		nvr = nvr + 1
		nc(nvr) = dble( count( H > 0))
		nc2(nvr) = nc(nvr)**2
		
		if ( nvr == navr) then
		
			s = sum( nc) / nvr
			s2 = sum( nc2) / nvr
			write(iunit,*) nmc, s, s2
			
			nc = 0.0d0
			nc2 = 0.0d0
			nvr = 0
		end if
			
	end do
	
	close( iunit)
	
	
end program main_corr
